/*
 * Copyright (c) 2016. jiiiiiin 15398699939@163.com
 */

package com.bankscene.screenfit.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.GravityCompat;
import android.view.MenuItem;

import com.bankscene.screenfit.R;
import com.bankscene.screenfit.model.Menus;
import com.bankscene.screenfit.dict.ErrorDict;
import com.bankscene.screenfit.events.EventBusEvents;
import com.bankscene.screenfit.fragment.MenusListViewFragment;
import com.bankscene.screenfit.util.ActivityUtils;
import com.bankscene.screenfit.vus.MenusVu;

import org.greenrobot.eventbus.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.bankscene.screenfit.activity.MainActivity.MENU_ITEM;
import static com.bankscene.screenfit.activity.MenusActivity.LEFT_FRAGMENT;
import static com.google.common.base.Preconditions.checkNotNull;

public abstract class AbstractMenusActivity<V extends MenusVu> extends BasePresenterActivity<V> {

    private Logger log = LoggerFactory.getLogger(AbstractMenusActivity.class);
    protected Menus chooseMenu;
    protected MenusListViewFragment menusListViewFragment;

    @Override
    protected void onBindVu() {
        super.onBindVu();
        Bundle paramsBundle = getIntent().getExtras();
        Object selectedMenusTemp = paramsBundle.get(MENU_ITEM);
        checkNotNull(selectedMenusTemp, ErrorDict.ERROR_SELECTEDMENUS_NOTBENULL);
        chooseMenu = (Menus) selectedMenusTemp;
        setTitle(chooseMenu.getTitle());

        // Set up the toolbar.
        setSupportActionBar(vu.getToolbar());
        vu.setToolbar(this, getSupportActionBar(), app.isPad());

        Object leftFragmentTemp = paramsBundle.get(LEFT_FRAGMENT);
        checkNotNull(leftFragmentTemp, ErrorDict.ERROR_LEFTFRAGMENT_NOTBENULL);
        try {
            if (app.isPad()) {
                menusListViewFragment = (MenusListViewFragment) Class.forName(String.valueOf(leftFragmentTemp)).newInstance();
                menusListViewFragment.setArguments(paramsBundle);
                ActivityUtils.addFragmentToActivity(fm, menusListViewFragment, R.id.left_fragment);
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                if (app.isPad()) {
                    super.onBackPressed();
                } else {
                    // Open the navigation drawer when the home icon is selected from the mainactivity_toolbar.
                    ((MenusVu) vu).getDrawerLayout().openDrawer(GravityCompat.START);
                }
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    //针对pad的菜单点击
    @Subscribe
    public void setOnItemClick(@NonNull EventBusEvents.OnItemClickEvent event) {
        Menus temp = Menus.getByPosition(event.position);
        setTitle(temp.getTitle());
        navigationItemSelectedDo(temp);
    }

    @Subscribe
    public void goHome(@NonNull EventBusEvents.Go2MainActivity go2MainActivity) {
        finish();
    }


    //针对Menus菜单被点击
    @Subscribe
    public void OnNavigationItemSelected(EventBusEvents.OnNavigationItemSelectedEvent event) {
        Integer menuId = event.menuItem.getItemId();
        Menus temp = Menus.getById(menuId);
        navigationItemSelectedDo(temp);
    }

    protected abstract void navigationItemSelectedDo(@NonNull Menus menus);

}
